package com.sneakxy.mybase4j.base.provider;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import com.sneakxy.mybase4j.base.repository.BaseRepository;
import com.sneakxy.mybatis.commons.repository.query.domain.Criteria;

/**
 * @author 潜行的青衣
 * @see
 */
public class BaseProvider<T, ID extends Serializable, R extends BaseRepository<T, ID>> implements IBaseProvider<T, ID> {

	protected R repository;
	
	@Autowired
	public BaseProvider(R repository) {
		this.repository = repository;
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#findAll()
	 */
	@Override
	public List<T> findAll() {
		return (List<T>) this.repository.findAll();
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#findAll(org.springframework.data.domain.Sort)
	 */
	@Override
	public List<T> findAll(Sort sort) {
		return (List<T>) this.repository.findAll(sort);
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#findAll(java.lang.Iterable)
	 */
	@Override
	public List<T> findAll(Iterable<ID> ids) {
		return (List<T>) this.repository.findAll(ids);
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#findAll(org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<T> findAll(Pageable pageable) {
		return this.repository.findAll(pageable);
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#findOne(java.io.Serializable)
	 */
	@Override
	public T findOne(ID id) {
		return this.repository.findOne(id);
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#exists(java.io.Serializable)
	 */
	@Override
	public boolean exists(ID id) {
		return this.repository.exists(id);
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#findOne()
	 */
	@Override
	public T findOne(Criteria<T> criteria) {
		return this.repository.findOne(criteria);
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#findAll()
	 */
	@Override
	public List<T> findAll(Criteria<T> criteria) {
		return this.repository.findAll(criteria);
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#findAll(, org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<T> findAll(Criteria<T> criteria, Pageable pageable) {
		return this.repository.findAll(criteria, pageable);
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#findAll(, org.springframework.data.domain.Sort)
	 */
	@Override
	public List<T> findAll(Criteria<T> criteria, Sort sort) {
		return this.repository.findAll(criteria, sort);
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#save(java.lang.Object)
	 */
	@Override
	public <S extends T> S save(S entity) {
		return this.repository.save(entity);
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#save(java.lang.Iterable)
	 */
	@Override
	public <S extends T> List<S> save(Iterable<S> entities) {
		return (List<S>) this.repository.save(entities);
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#delete(java.io.Serializable)
	 */
	@Override
	public void delete(ID id) {
		this.repository.delete(id);
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#delete(java.lang.Object)
	 */
	@Override
	public void delete(T entity) {
		this.repository.delete(entity);
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#delete(java.lang.Iterable)
	 */
	@Override
	public void delete(Iterable<? extends T> entities) {
		this.repository.delete(entities);
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#deleteAll()
	 */
	@Override
	public void deleteAll() {
		this.repository.deleteAll();
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#count()
	 */
	@Override
	public long count(Criteria<T> criteria) {
		return this.repository.count(criteria);
	}

	/* (non-Javadoc)
	 * @see com.mybase4j.base.provider.IBaseProvider#count()
	 */
	@Override
	public long count() {
		return this.repository.count();
	}

	public R getRepository() {
		return repository;
	}
	
}
